/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.j002.requirementengineeringmuni.swing;

import java.util.ArrayList;
import java.util.List;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author Stefan.Rakonjac
 */
public class HighScoreJTableModel extends AbstractTableModel {
    
    List<Score> scores = new ArrayList<Score>();

    public int getRowCount() {
        return scores.size();
    }

    public int getColumnCount() {
        return 2;
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        Score score = scores.get(rowIndex);
        if(score == null) return null;
        return 0 == columnIndex ? score.getName() : score.getValue();
    }
    
    public boolean addScore(String name, int value) {
        Score lowestScore = null;
        for(Score score : scores) {
            if(null == lowestScore || lowestScore.getValue() >= score.getValue()) lowestScore = score;
        }
        
        if(lowestScore.getValue() < value) {
            scores.remove(lowestScore);
            scores.add(new Score(name, value));
            return true;
        }
        
        return false;
    }
    
    
    class Score {
        String name;
        int value;
        Score(String name, int Value) {
            this.name = name;
            this.value = value;
        }  
        public String getName() { return name; }
        public int getValue() { return value; }
    }
}
